package ywpt.service.config;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.Department;
import ywpt.model.GroupInfo;
import ywpt.web.common.PublicFunction;

public class GroupServiceImpl  extends HibernateEntityDao<GroupInfo> implements GroupService{
	public GroupServiceImpl(){
		
	}

	@Override
	public String createGroupCode() {
		DetachedCriteria query = DetachedCriteria.forClass(GroupInfo.class, "group");
		String groupCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<GroupInfo> groupInfos = this.getGroupInfos(query);
		if ( groupInfos.iterator().hasNext() )	
		{
			code = groupInfos.iterator().next().getCode();
			maxId = new Integer(code);
		}
		groupCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return groupCode;
	}

	@Override
	public GroupInfo getGroupInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getGroupInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GroupInfo> getGroupInfos(DetachedCriteria query, int pageSize,
			int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<GroupInfo> getGroupInfos() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GroupInfo> getGroupInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public void removeGroupInfo(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveGroupInfo(GroupInfo groupInfo,Long departId) {
		if(departId != null ){
			groupInfo.setDepart(this.get(Department.class,departId));
		}
		this.save(groupInfo);
	}

	@Override
	public void updateGroupInfo(GroupInfo groupInfo,Long departId) {
		if(departId != null ){
			groupInfo.setDepart(this.get(Department.class,departId));
		}		
		this.update(groupInfo);
	}
}
